package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldProjectMaterial;
import com.cleaningcloud.cld.domain.vo.CldProjectMaterialVo;
import com.cleaningcloud.cld.mapper.CldProjectMaterialMapper;
import com.cleaningcloud.cld.service.ICldProjectMaterialService;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.erp.api.RemoteCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目物资阈值设置Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-15
 */
@Service
public class CldProjectMaterialServiceImpl implements ICldProjectMaterialService {
    @Autowired
    private CldProjectMaterialMapper cldProjectMaterialMapper;

    @Autowired
    private RemoteCommodityService remoteCommodityService;

    /**
     * 查询项目物资阈值设置
     *
     * @param projectMaterialId 项目物资阈值设置主键
     * @return 项目物资阈值设置
     */
    @Override
    public CldProjectMaterial selectCldProjectMaterialByProjectMaterialId(Long projectMaterialId) {
        return cldProjectMaterialMapper.selectCldProjectMaterialByProjectMaterialId(projectMaterialId);
    }

    /**
     * 查询项目物资阈值设置列表
     *
     * @param cldProjectMaterial 项目物资阈值设置
     * @return 项目物资阈值设置
     */
    @Override
    public List<CldProjectMaterial> selectCldProjectMaterialList(CldProjectMaterial cldProjectMaterial) {
        //cldProjectMaterial.setCommodityType(CommunityTypeConversionUtils.conversionRegexp(cldProjectMaterial.getCommodityType()));
        if(null != cldProjectMaterial.getCommodityType()){
            cldProjectMaterial.setCommodityType(cldProjectMaterial.getCommodityType().replace('|',','));
        }
        List<CldProjectMaterial> list = cldProjectMaterialMapper.selectCldProjectMaterialList(cldProjectMaterial);
        if(!list.isEmpty()){
            for(CldProjectMaterial c : list){
                c.setThresholdNumber(c.getCommodityNum());
                c.setWareCommodityNumber(Long.parseLong(Integer.valueOf(c.getWareNumber()).toString()));
            }
        }
        return list;
    }

    /**
     * 新增项目物资阈值设置
     *
     * @param cldProjectMaterial 项目物资阈值设置
     * @return 结果
     */
    @Override
    public int insertCldProjectMaterial(CldProjectMaterialVo projectMaterialVo) {
        //删除原有的数据
        cldProjectMaterialMapper.deleteCldProjectMaterialByProjectId(projectMaterialVo.getProjectId());
        for (CldProjectMaterial material : projectMaterialVo.getMaterialList()) {
            material.setProjectId(projectMaterialVo.getProjectId());
            if (material.getCommodityNum() == null || material.getCommodityNum() <= 0) {
                throw new RuntimeException("商品编号：" + material.getCommodityNo() + ",商品名称：" + material.getCommodityName1() + ",配送阀值不能小于等于0");
            }
        }
        return cldProjectMaterialMapper.batchInsert(projectMaterialVo.getMaterialList());
    }


}
